/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.members.impl;

import com.qinyeit.serviceapp.entity.BatchCardRecord;
import com.qinyeit.serviceapp.entity.MemberCard;
import com.qinyeit.serviceapp.repository.members.BatchCardRecordRepository;
import com.qinyeit.serviceapp.service.members.BatchCardRecordService;
import com.qinyeit.serviceapp.service.members.MemberCardService;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.repository.jdbc.PageNamedParameterJdbcTemplate;
import com.qinyetech.springstage.core.service.BaseService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/** 
 * ClassName: BatchCardRecordServiceImpl <br/> 
 * Function: 批量制卡记录 业务实现 <br/> 
 * date: 2018-12-04 11:14:20 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class BatchCardRecordServiceImpl extends BaseService<BatchCardRecord, Long> implements BatchCardRecordService {


	@Autowired
	private BatchCardRecordRepository batchCardRecordRepository;
	@Autowired
	private PageNamedParameterJdbcTemplate jdbcTemplate;
	@Autowired
	private MemberCardService memberCardService;

	@Override
	public Long getUsableCard(Long branchId,Long merchantGroupId) {
		String sql = "SELECT count(*) as count from member_cards where merchant_group_id = :merchantGroupId and branch_id = :branchId and vip_id is null";
		QyMap map = QyMap.map("merchantGroupId",merchantGroupId).setv("branchId",branchId);
		Long count = jdbcTemplate.queryForScalar(sql,map,Long.class);
		return count;
	}

	@Override
	public QyMap getNoRange(Long batchId,Integer count, Long merchantGroupId) {
		QyMap qyMap = QyMap.NEW();
		String sql = "SELECT no from member_cards where batch_id = :batchId and branch_id is null ORDER BY no asc LIMIT 0,:count";
		QyMap map = QyMap.map("batchId",batchId).setv("count",count);
		List<Map<String,Object>> mapList = jdbcTemplate.queryForList(sql,map);
		if(CollectionUtils.isNotEmpty(mapList)){
			qyMap.setv("startNo",mapList.get(0).get("no"));
			qyMap.setv("endNo",mapList.get(mapList.size()-1).get("no"));
		}
		return qyMap;
	}

	@Override
	public void deleteData(Long batchId) {
//		b)物理删除会员卡和制卡记录
		String sql = "DELETE from member_cards where batch_id = :batchId";
		QyMap map = QyMap.map("batchId",batchId);
		jdbcTemplate.update(sql,map);

		sql = "DELETE from batch_card_records where id = :batchId";
		jdbcTemplate.update(sql,map);
	}

	@Override
	public void saveData(BatchCardRecord batchCardRecord) {
		batchCardRecord = this.update(batchCardRecord);
		memberCardService.saveBatchCard(batchCardRecord);
	}

}